package com.nuoyun.tnvmue.activity.fragment


import android.app.Activity
import android.os.Build
import android.view.View
import android.view.WindowManager
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import com.nuoyun.tnvmue.R
import com.nuoyun.tnvmue.activity.HelpActivity
import com.nuoyun.tnvmue.activity.NewsActivity
import com.nuoyun.tnvmue.utils.LogUtil
import com.nuoyun.tnvmue.utils.ToolUtils
import kotlinx.android.synthetic.main.view_title_bar.*
import kotlinx.android.synthetic.main.view_title_bar.title
import kotlinx.android.synthetic.main.view_title_bar.view_title_bar
import kotlinx.android.synthetic.main.view_title_bar_1.*

/**
 * @author JiangAdmin
 * date: 2020/4/10
 * Email: jiangmr@vip.qq.com
 * TODO:
 */

open class BaseFragment : Fragment() {

    /**
     * ToolBar样式
     *
     * @param type 0 白  1 左右渐变变 2 上下渐变
     */
    fun toolBarStyle(view: View, type: Int) {
        when (type) {
            //白色
            0 -> {
                view_title_bar.setBackgroundColor(ContextCompat.getColor(activity!!, R.color.white))
                title.setTextColor(ContextCompat.getColor(activity!!, R.color.gray_3))
                back.setImageResource(R.drawable.ic_back_g)
                menu.setTextColor(ContextCompat.getColor(activity!!, R.color.gray_3))
                setDarkStatusIcon(true)
            }
            //主题色
            1 -> {
                view_title_bar.setBackgroundResource(R.color.style_color)
                title.setTextColor(ContextCompat.getColor(activity!!, R.color.white))
                back.setImageResource(R.drawable.ic_back)
                menu.setTextColor(ContextCompat.getColor(activity!!, R.color.white))
                setDarkStatusIcon(false)
            }
        }
    }

    /**
     * 是否显示帮助
     */
    fun showHelp(show: Boolean) {
        if (show) {
            help.visibility = View.VISIBLE
            help.setOnClickListener { HelpActivity.start(activity!!) }
        } else {
            help.visibility = View.GONE

        }
    }

    /**
     * 是否显示信息
     */
    fun showNews(show: Boolean) {
        if (show) {
            news.visibility = View.VISIBLE
            news.setOnClickListener { NewsActivity.start(activity!!) }
        } else {
            news.visibility = View.GONE
        }
    }

    /**
     * 自动调整标题高度
     *
     * @param view
     */
    fun autoTitleHeight(view: View) {
        val layoutParams = view.layoutParams
        //获取状态栏高度 加上 要设置的标题栏高度 等于 标题栏实际高度
        layoutParams.height = ToolUtils.getStatusHeight() + ToolUtils.dp2px(44f)
        view.layoutParams = layoutParams
    }


    /**
     * 状态栏 亮暗
     *
     * @param bDark
     */
    fun setDarkStatusIcon(bDark: Boolean) {

        //如果是Android 6.0以上
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            val decorView = activity!!.window.decorView
            if (decorView != null) {
                var vis = decorView.systemUiVisibility
                vis = if (bDark) {
                    vis or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
                } else {
                    vis and View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR.inv()
                }
                decorView.systemUiVisibility = vis
            }
        }
        val deviceMan = Build.MANUFACTURER
        if ("Xiaomi" == deviceMan) {
            setMiuiStatusBarDarkMode(activity!!, bDark)

        } else if ("meizu" == deviceMan) {
            setMeizuStatusBarDarkIcon(activity, bDark)
        }

    }

    /**
     * 设置标题
     *
     * @param title
     */
    fun setTitle(view: View, title: Any) {

        val tv = view.findViewById<TextView>(R.id.title)

        if (title is String) {
            tv.text = title
        }

        if (title is Int) {
            tv.text = resources.getString(title)
        }

    }

    /**
     * 设置菜单
     *
     * @param objects 图片或者文字/文字或者颜色/颜色
     */
    fun setMenu(view: View, vararg objects: Any) {

        if (objects.isNotEmpty()) {
            menu.visibility = View.VISIBLE
            menu.setOnClickListener(this as View.OnClickListener)
            //判断是不是图
            if (objects[0] is Int) {

                val drawableleft = ContextCompat.getDrawable(activity!!, objects[0] as Int)
                drawableleft!!.setBounds(0, 0, drawableleft.intrinsicWidth, drawableleft.minimumHeight)
                //只放左边
                menu!!.setCompoundDrawables(drawableleft, null, null, null)

                //判断是不是字
                if (objects.size > 1 && objects[1] is String) {

                    menu.text = objects[1] as String

                    //判断是不是颜色
                    if (objects.size > 2 && objects[2] is Int) {

                        menu.setTextColor(ContextCompat.getColor(activity!!, objects[2] as Int))

                    }
                }
            }

            //判断是不是字
            if (objects[0] is String) {

                menu.text = objects[0] as String
                //判断是不是颜色
                if (objects.size > 1 && objects[1] is Int) {
                    menu.setTextColor(ContextCompat.getColor(activity!!, objects[1] as Int))
                }
            }
        } else {
            menu.visibility = View.GONE
        }
    }

    companion object {
        private val TAG = "BaseFragment"

        /**
         * MIUI 系统
         *
         * @param activity
         * @param darkmode 字体颜色为黑色。
         * @return
         */
        fun setMiuiStatusBarDarkMode(activity: Activity, darkmode: Boolean): Boolean {
            val clazz = activity.window.javaClass
            try {
                var darkModeFlag = 0
                val layoutParams = Class.forName("android.view.MiuiWindowManager\$LayoutParams")
                val field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE")
                darkModeFlag = field.getInt(layoutParams)
                val extraFlagField = clazz.getMethod("setExtraFlags", Int::class.javaPrimitiveType, Int::class.javaPrimitiveType)
                extraFlagField.invoke(activity.window, if (darkmode) darkModeFlag else 0, darkModeFlag)
                return true
            } catch (e: Exception) {
                e.printStackTrace()
            }

            return false
        }

        /**
         * Flyme 系统
         *
         * @param activity
         * @param dark     字体颜色
         * @return
         */
        fun setMeizuStatusBarDarkIcon(activity: Activity?, dark: Boolean): Boolean {
            var result = false
            if (activity != null) {
                try {
                    val lp = activity.window.attributes
                    val darkFlag = WindowManager.LayoutParams::class.java
                            .getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON")
                    val meizuFlags = WindowManager.LayoutParams::class.java
                            .getDeclaredField("meizuFlags")
                    darkFlag.isAccessible = true
                    meizuFlags.isAccessible = true
                    val bit = darkFlag.getInt(null)
                    var value = meizuFlags.getInt(lp)
                    if (dark) {
                        value = value or bit
                    } else {
                        value = value and bit.inv()
                    }
                    meizuFlags.setInt(lp, value)
                    activity.window.attributes = lp
                    result = true
                } catch (e: Exception) {
                    LogUtil.e(TAG, e.message)
                }

            }
            return result
        }

    }
}
